#include "compile_run.h"
#include "../comm/httplib.h" //第三方库http
using namespace ns_CompileAndRun;

void test_local_cr() // 本地编译运行测试
{
    // 读取需要编译的文件数据内容+序列化
    Json::Value root;
    ifstream in("json.cpp");
    if (!in.is_open())
        cout << "文件未打开" << endl;
    string code, line;
    while (getline(in, line))
    {
        code += (line + "\n");
    }

    root["code"] = code;
    root["cpu_limit"] = 1;
    root["mem_limit"] = 102400;

    Json::FastWriter writer; // 无格式
    string json_in = writer.write(root);

    // 运行
    string ret;
    compile_and_runner::StartCompileAndRunner(json_in, &ret);
    cout << ret << endl;
}

using namespace httplib;
void test_httplib() // 认识httplib库的用法
{
    Server svr;
    svr.Get("/haha", [](const Request &req, Response &rsp) -> void
            { rsp.set_content("hahahahaha,你好!!!", "text/plain;charset=UTF-8"); });
    // svr.set_base_dir()
    svr.listen("0.0.0.0", 8888); // 启动http服务器
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        cout << "\n\t usage: argv[0] port" << endl;
    }
    int port = atoi(argv[1]);

    Server svr;
    // 1.通过post方法,将代码上传到请求报文的content中,然后编译运行代码
    svr.Post("/CompileAndRun", [](const Request &req, Response &rsp)
             {
        string json_in,json_out;
        json_in = req.body;
        compile_and_runner::StartCompileAndRunner(json_in, &json_out);
        rsp.set_content(json_out,"applicaton/json;charset=utf-8"); });

    // 2.启动http服务器
    svr.listen("0.0.0.0", port); // 启动http服务器

    return 0;
}